﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace _2___StudentRecord
{
    class Program
    {
        static void AddStudent(string fName, string lName, int fNum)
        {
            BinaryWriter writer = new BinaryWriter(File.Open("students.bin", FileMode.Append));
            using (writer)
            {
                writer.Write(fName);
                writer.Write(lName);
                writer.Write(fNum);
            }
        }

        static void DeleteStudent(int num)
        {
            BinaryReader reader = new BinaryReader(File.Open("students.bin", FileMode.Open));
            using (reader)
            {
                BinaryWriter writer = new BinaryWriter(File.Open("students2.bin", FileMode.Create));
                using (writer)
                {
                    string name;
                    string second;
                    int number;

                    while (reader.BaseStream.Position < reader.BaseStream.Length)
                    {
                        name = reader.ReadString();
                        second = reader.ReadString();
                        number = reader.ReadInt32();
                        if (number != num)
                        {
                            writer.Write(name);
                            writer.Write(second);
                            writer.Write(number);
                        }
                    }
                }
            }
            File.Delete("students.bin");
            File.Move("students2.bin", "students.bin");
        }

        static void PrintStudents()
        {
            BinaryReader reader = new BinaryReader(File.Open("students.bin", FileMode.Open));
            using (reader)
            {
                string name;
                string second;
                int number;

                while (reader.BaseStream.Position < reader.BaseStream.Length)
                {
                    name = reader.ReadString();
                    second = reader.ReadString();
                    number = reader.ReadInt32();
                    Console.WriteLine("{0} {1} {2}", name, second, number);                    
                }
            }
        }

        static void Main(string[] args)
        {
            AddStudent("bobby", "kirilov", 23);
            AddStudent("milena", "kirilova", 24);
            AddStudent("alex", "tonkin", 21);
            AddStudent("bobby", "kirilov", 23);
            AddStudent("milena", "kirilova", 24);
            AddStudent("alex", "tonkin", 21);
            AddStudent("bobby", "kirilov", 23);
            AddStudent("milena", "kirilova", 24);
            AddStudent("alex", "tonkin", 21);
            AddStudent("bobby", "kirilov", 23);
            AddStudent("milena", "kirilova", 24);
            AddStudent("alex", "tonkin", 21);
            AddStudent("bobby", "kirilov", 23);
            AddStudent("milena", "kirilova", 24);
            AddStudent("alex", "tonkin", 21);
            AddStudent("bobby", "kirilov", 23);
            AddStudent("milena", "kirilova", 24);
            AddStudent("alex", "tonkin", 21);
            AddStudent("bobby", "kirilov", 23);
            AddStudent("milena", "kirilova", 24);
            AddStudent("alex", "tonkin", 21);
            AddStudent("bobby", "kirilov", 23);
            AddStudent("milena", "kirilova", 24);
            AddStudent("alex", "tonkin", 21);
            AddStudent("bobby", "kirilov", 23);
            AddStudent("milena", "kirilova", 24);
            AddStudent("alex", "tonkin", 21);
            AddStudent("bobby", "kirilov", 23);
            AddStudent("milena", "kirilova", 24);
            AddStudent("alex", "tonkin", 21);
            AddStudent("bobby", "kirilov", 23);
            AddStudent("milena", "kirilova", 24);
            AddStudent("alex", "tonkin", 21);
            AddStudent("bobby", "kirilov", 23);
            AddStudent("milena", "kirilova", 24);
            AddStudent("alex", "tonkin", 21);
            AddStudent("bobby", "kirilov", 23);
            AddStudent("milena", "kirilova", 24);
            AddStudent("alex", "tonkin", 21);
            AddStudent("bobby", "kirilov", 23);
            AddStudent("milena", "kirilova", 24);
            AddStudent("alex", "tonkin", 21);
            AddStudent("bobby", "kirilov", 23);
            AddStudent("milena", "kirilova", 24);
            AddStudent("alex", "tonkin", 21);
            AddStudent("bobby", "kirilov", 23);
            AddStudent("milena", "kirilova", 24);
            AddStudent("alex", "tonkin", 21);
            AddStudent("bobby", "kirilov", 23);
            AddStudent("milena", "kirilova", 24);
            AddStudent("alex", "tonkin", 21);
            AddStudent("bobby", "kirilov", 23);
            AddStudent("milena", "kirilova", 24);
            AddStudent("alex", "tonkin", 21);
            AddStudent("bobby", "kirilov", 23);
            AddStudent("milena", "kirilova", 24);
            AddStudent("alex", "tonkin", 21);
            AddStudent("bobby", "kirilov", 23);
            AddStudent("milena", "kirilova", 24);
            AddStudent("alex", "tonkin", 21);
            AddStudent("bobby", "kirilov", 23);
            AddStudent("milena", "kirilova", 24);
            AddStudent("alex", "tonkin", 21);
            AddStudent("bobby", "kirilov", 23);
            AddStudent("milena", "kirilova", 24);
            AddStudent("alex", "tonkin", 21);
            AddStudent("bobby", "kirilov", 23);
            AddStudent("milena", "kirilova", 24);
            AddStudent("alex", "tonkin", 21);
            PrintStudents();
            DeleteStudent(23);
            Console.WriteLine();
            Console.WriteLine();
            PrintStudents();
        }
    }
}
